﻿/// <reference path="../jquery-1.10.2.js" />
/// <reference path="../Controls/DropDownBtn.js" />
/// <reference path="../Controls/ComboBox/ComboBox.js" />
/// <reference path="../Controls/ControlManager.js" />

//jquery的方法扩展
jQuery.extend(jQuery, {
    getJQObj: function (param) {
        /// <summary>获取JQ对象</summary>
        /// <param name="param" type="DOMElement/String[id]">dom元素对象或者是元素的ID号</param>
        var obj = null;
        var sType = typeof (param);
        if (sType === "string") obj = $("#" + param);
        else if (sType === "object") obj = $(param);
        return obj;
    },
    create: function (type, config) {
        /// <summary>根据控件类型创建一个控件</summary>
        /// <param name="type" type="String">控件类型</param>
        /// <param name="config" type="JSONObject">配置数据</param>
        return ControlManager.create(type, config);
    },
    disabledInputOp: function () {
        /// <summary>禁止input标签输入的方法</summary>
        return false;
    },
    canAppendChild: function (el) {
        /// <summary>判断对象是否可以append子对象</summary>
        /// <param name="param" type="DOMElement/String[id]">dom元素对象或者是元素的ID号</param>
        if (typeof (el) === "undefined") return false;
        var obj = $.getJQObj(el);
        if (obj === null) return false;
        var tagName = obj[0].tagName;
        if (tagName === "INPUT") return false;
        return true;
    },
    htmlEncode: function (str) {
        /// <summary>对html进行编码，否则会有安全性问题</summary>
        var div = document.createElement("div");
        var text = document.createTextNode(str);
        div.appendChild(text);
        return div.innerHTML;
    },
    htmlDecode: function (str) {
        /// <summary>对html进行解码</summary>
        var div = document.createElement("div");
        div.innerHTML = str;
        return div.innerHTML;
    },
    zIndex: 1 //用于控制全局的zIndex层次关系
});

//扩展offset()方法，可以指定相对的父对象，如果不指定，则相对body的位置
jQuery.fn.offsetEx = function (parent) {
    /// <summary>根据指定的父元素获取元素的偏移位置</summary>
    /// <param name="parent" type="DomObj">Dom元素对象</param>
    if (!parent) {
        var offset = this.offset();
        //-2是因为jq计算有误差，不知道哪里出了问题
        return { top: offset.top - 2, left: offset.left - 2 };
    }
    else {
        var oNode = this[0];
        var iTop = 0;
        var iLeft = 0;
        var iCount = 1;
        while (oNode && oNode != parent) {
            iTop += oNode.offsetTop - oNode.scrollTop;
            iLeft += oNode.offsetLeft - oNode.scrollLeft;
            oNode = oNode.offsetParent;
        }
        return { top: iTop, left: iLeft };
    }
};

jQuery.fn.initControl = function () {
    /// <summary>初始化所有控件，生成所有的控件</summary>
    //初始化combobox
    $(this).find('input[ctype=combobox]').setComboBox();
};

//根据坐标，获取坐标上的元素对象
(function ($) {
    var check = false,
	isRelative = true;
    $.elementFromPoint = function (x, y) {
        if (!document.elementFromPoint) return null;
        if (!check) {
            var sl;
            if ((sl = $(document).scrollTop()) > 0) {
                isRelative = (document.elementFromPoint(0, sl + $(window).height() - 1) == null);
            } else if ((sl = $(document).scrollLeft()) > 0) {
                isRelative = (document.elementFromPoint(sl + $(window).width() - 1, 0) == null);
            }
            check = (sl > 0);
        }
        if (!isRelative) {
            x += $(document).scrollLeft();
            y += $(document).scrollTop();
        }
        return document.elementFromPoint(x, y);
    }
})(jQuery);